namespace SB2.Core;


public class SysVersionRepository : SugarRepository<SysVersion>
{
    public SysVersionRepository(ISqlSugarClient context) : base(context)
    {

    }
    public async Task<long> GetLastVersion()
    {
        return await AsQueryable().MaxAsync(m => m.VerisionId);
    }
    public async Task<bool> UpdateVersion(string sql, long version)
    {
        bool res = false;
        var result = (await Context.Ado.UseTranAsync(async () =>
        {
            await Context.Ado.ExecuteCommandAsync(sql);
            await InsertAsync(new SysVersion { VerisionId = version, ExcTime = DateTime.Now });
            res = true;
        }, err =>
        {
            Console.WriteLine(err.ToString());
            res = false;
        }));
        Console.WriteLine(result.Data);
        return res && result.Data;
    }
    public override void Init()
    {
        if (!Context.DbMaintenance.IsAnyTable("sbversion"))
        {
            base.Init();
            this.Context.CodeFirst.InitTables(typeof(User));
            var user = User.GetDefault();
            if (!Context.Queryable<User>().Any(m => m.Name == user.Name))
                Context.Insertable<User>(user).ExecuteCommand();
        }
    }
}